const app = getApp();
const log =require('../../log.js');
const util=require('../../util.js');
const tabService = require("../../libs/tabService");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        array: ['临沂', '济宁', '菏泽', '枣庄'],
        city:'请选择城市',
        arrays:[
            {name: '贷款', value: '贷款', checked: 'true'},
            {name: '全款', value: '全款'},

        ],
        need:0,
        second:5,
        show:0,
        check:false
    
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var id = options.id;
        console.log(id);
        if(/^\d+$/.test(id)){
            this.setData({
                id:id
            })
            this.getTruckOrder(id);
        }else{
            console.log('id='+id);
        }
        let that = this ;
        let d =   app.globalData;  
        that.setData({
            platform:d.platform,    
           // userInfo:userInfo,
             width:d.windowWidth,
            height:d.windowHeight,
            statusBarHeight:d.statusBarHeight,
            navHeight:d.navHeight,
            marginTop:d.navTitleTop,
            domain:d.domain,
            uploadUrl:d.uploadUrl
          })
    },

    goBack:function(){
        wx.navigateBack();
      },
      goIndex:function(){
        wx.reLaunch({
          url: '../index/index',
        })
      },
      getTruckOrder(id){
        let that = this;
        var userInfo =  wx.getStorageSync('userInfo');
        var data = [];
        data['user_id']= userInfo.id;
        data['id'] = id;
        var url = 'clear/getTruckOrder';

      app.request(url,data,'get').then(res=>{ 
        console.log('customer getIndex'); 
         console.log(res);
     
         //this.initChart(res.pie);
         that.setData({
             
             'truck':res.truck,
           
            
             
         });

       //  that.wxUpdate(); //that.wxLogIn();  
      })

      },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    inputName(e){
        var value= e.detail.value;
        this.setData({
            name:value
        })
    },
    inputPhone(e){
        var value= e.detail.value;
        this.setData({
            phone:value
        })
    },
    inputIdcard(e){
        var value= e.detail.value;
        this.setData({
            idcard:value
        })
    },
    cityChange(e){
        console.log(e);
        var value = e.detail.value;
        this.setData({
            city:value
        })
    },
    loanChange(e){
        var value = e.detail.value;
        console.log(e);
        this.setData({
            loan:value
        })
    },
    needChange(e){
        //console.log(e);
        let that = this;
        var need = this.data.need;
        if(need == 1){
            need = 1;
        }else{
            need = 1;
        }
        this.setData({
            need:need,
            show:1
        })
        that.data.timer = setInterval(
            function () {
        that.toClock();        
        }, 1000);

        that.setData({
            timer:that.data.timer
        });
    },
    toClock(){
        var second = this.data.second;
        if(second>0){
            //clearInterval(this.data.timer);
            second = second -1;
            this.setData({
                second
            })
         
        }else{
            clearInterval(this.data.timer);
        }
    },
    hide(){
        this.setData({
            show:0,
            check:true
        })
    },
    onSubmit(){
         let that = this;
         let d = this.data;
        
         if(d.check === false){
            wx.showModal({
                title: '重要提请',
                content: '请阅读车辆预定须知',
                            
              })
              return;
         }
         if(d.name == undefined){
            wx.showModal({
              title: '重要提请',
              content: '请输入车主姓名',
                          
            })
            return;
         }
         const regex = /^[1]{1}\d{10}$/;
         if(d.phone == undefined){
            wx.showModal({
                title: '重要提请',
                content: '请输入联系电话',
                            
              })
              return;
         }else if(!regex.test(d.phone)){
            wx.showModal({
                title: '重要提请',
                content: '手机号输入错误',
                            
              })
              return;
         }
         if(d.city == '请选择城市'){
            wx.showModal({
                title: '重要提请',
                content: '请选择城市',
                            
              })
              return;
         }
         var userInfo =  wx.getStorageSync('userInfo');
         let store_id = wx.getStorageSync('store_id');
         var data = [];
         data['store_id'] = store_id;
         data['user_id'] = userInfo.id;
         data['openid'] = userInfo.mini_openid;
         data['id'] = d.id;
         data['vin'] = d.truck.vin;
         data['name'] = d.name;
         data['phone'] = d.phone;
         data['idcard'] = d.idcard;
         data['loan'] = d.loan;
         data['city'] = d.city;
         var url = 'clear/order';
         app.request(url,data,'get').then(res=>{ 
           console.log('clear order');
           console.log(res);
           var code=res.code;
           if(code==200){
           var data=res.data;
             wx.requestPayment({
                 'timeStamp': data.timeStamp,
                 'nonceStr': data.nonceStr,
                 'package': data.package,
                 'signType': 'MD5',
                 'paySign': data.paySign,
                 'success': function (res) {
                  // console.log('success');
                   wx.showToast({
                     title: '支付成功',
                     icon: 'success',
                     duration: 3000
                   });
                   setTimeout(function() {
                       //要延时执行的代码
                       wx.reLaunch({
                   
                         url: '../mine/index',
                       })
                     }, 1000) //延迟时间
                   //that.waitPayShow();
                 },
                 'fail': function (res) {
                   //console.log(res);
                 },
                 'complete': function (res) {
                  // console.log('complete');
                 }
               });
           }else{
            wx.showToast({
                title: '车辆已被预定',
                icon: 'error',
                duration: 3000
              });
           }
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})